import Decorator from './decorator';

export default class CompositeDecorator {
  constructor(decorators) {
    if (!Array.isArray(decorators)) {
      throw new Error(
        `CompositeDecorator constructor accept array decorators, but got ${decorators}`
      );
    }
    this.decorators = decorators.map(item => new Decorator(item));
  }

  render(props, element) {
    let result = element;
    this.decorators.forEach(decorator => {
      result = decorator.render(props, result);
    });
    return result;
  }
}
